package com.itcam.cm.user.controller;


import com.itcam.cm.model.dto.user.UserLoginDto;
import com.itcam.cm.model.dto.user.UserRegisterDto;
import com.itcam.cm.model.entity.user.AuthContextUtil;
import com.itcam.cm.model.entity.user.UserInfo;
import com.itcam.cm.model.vo.common.Result;
import com.itcam.cm.model.vo.user.UserInfoVo;
import com.itcam.cm.user.service.UserInfoService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/api/user/userInfo")
@RestController
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;

    @PostMapping("register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto){
        userInfoService.register(userRegisterDto);
        return Result.ok(null);
    }

    @PostMapping("login")
    public Result login(@RequestBody UserLoginDto userLoginDto, HttpServletRequest request) {
        // String ip = IpUtil.getIpAddress(request);
        String ip = request.getRemoteAddr();
        String token = userInfoService.login(userLoginDto, ip);
        return Result.ok(token);
    }

    @GetMapping("/auth/checkUserInfo/{token}")
    public UserInfo checkUserInfo(HttpServletRequest request,@PathVariable String token){
        // String token = request.getHeader("token");
        UserInfo userInfo = userInfoService.checkUserInfo(token);
        return userInfo;
    }

    @GetMapping("auth/getCurrentUserInfo")
    public Result<UserInfoVo> getCurrentUserInfo(HttpServletRequest request) {
        // String token = request.getHeader("token");
        String userJson = request.getHeader("userJson");
        System.out.println(userJson);
        // 本地线程获取用户信息
        UserInfo user = AuthContextUtil.getUser();
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setNickName(user.getNickName());
        userInfoVo.setAvatar(user.getAvatar());
        return Result.ok(userInfoVo) ;
    }


}
